#include "mainwindow.h"

#include <QCheckBox>
#include <QDebug>
#include <QFormLayout>
#include <QGraphicsView>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QStackedWidget>

#include "ElaContentDialog.h"
#include "ElaEventBus.h"
#include "ElaPushButton.h"
#include "ElaLineEdit.h"
#include "ElaText.h"
#include "ElaCheckBox.h"
#include "ElaWidget.h"
#include "borrowreturnwidget.h"

#include "loginwidget.h"
#include "signinwidget.h"

#include "ExamplePage/T_Home.h"

#include <QTimer>

#include "logindialog.h"
#include "readermanagerwidget.h"

#include "bookquerywidget.h"
#include "deletebuttondelegate.h"

MainWindow::MainWindow(QWidget *parent)
    : ElaWindow(parent)
{
    initWindow();

    //额外布局
    initEdgeLayout();

    //中心窗口
    initContent();

    // 拦截默认关闭事件
    m_closeDialog = new ElaContentDialog(this);
    connect(m_closeDialog, &ElaContentDialog::rightButtonClicked, this, &MainWindow::closeWindow);
    connect(m_closeDialog, &ElaContentDialog::middleButtonClicked, this, [=]() {
        m_closeDialog->close();
        showMinimized();
    });
    this->setIsDefaultClosed(false);
    connect(this, &MainWindow::closeButtonClicked, this, [=]() {
        m_closeDialog->exec();
    });
    //移动到中心
    moveToCenter();
}

MainWindow::~MainWindow() {}

void MainWindow::initWindow()
{
    setWindowIcon(QIcon(":/resource/Icon/document-e.png"));
    resize(1200, 740);

    setUserInfoCardPixmap(QPixmap(":/resource/Icon/document-e.png"));
    setUserInfoCardTitle("用户名");

    setUserInfoCardSubTitle("Liniyous@gmail.com");
    setWindowTitle("基于RFID的图书馆管理系统");

}

void MainWindow::initEdgeLayout()
{

}

void MainWindow::initContent()
{
    ReaderManagerWidget *widget = new ReaderManagerWidget(this);
    addPageNode("读者管理", widget, ElaIconType::BookOpenReader);

    connect(this, &MainWindow::userInfoCardClicked, this, [=]() {
        this->navigation(widget->property("ElaPageKey").toString());
    });
    widget->setWindowIcon(QIcon(":/resource/Icon/reader_manager.png"));


    QWidget *widget_2 = new QWidget(this);
    addPageNode("登录管理", widget_2, ElaIconType::BookArrowUp);

    connect(this, &MainWindow::userInfoCardClicked, this, [=]() {
        this->navigation(widget_2->property("ElaPageKey").toString());
    });


    BorrowReturnWidget *widget_3 = new BorrowReturnWidget(this);
    addPageNode("借还书管理", widget_3, ElaIconType::BookCircleArrowRight);

    connect(this, &MainWindow::userInfoCardClicked, this, [=]() {
        this->navigation(widget_3->property("ElaPageKey").toString());
    });


    QWidget *widget_4 = new QWidget(this);
    addPageNode("书库管理", widget_4, ElaIconType::Books);

    connect(this, &MainWindow::userInfoCardClicked, this, [=]() {
        this->navigation(widget_4->property("ElaPageKey").toString());
    });


    BookQueryWidget *widget_5 = new BookQueryWidget(this);
    addPageNode("查询管理", widget_5, ElaIconType::MagnifyingGlass);

    connect(this, &MainWindow::userInfoCardClicked, this, [=]() {
        this->navigation(widget_5->property("ElaPageKey").toString());
    });

    qDebug() << "已注册的事件列表" << ElaEventBus::getInstance()->getRegisteredEventsName();
}

void MainWindow::onLoginSuccess(const QString &username, const QString &password)
{
    qDebug()  << "用户:" << username << " 密码:" << password << "登录成功!";

    this->show();
    emit windowShown();
}

void MainWindow::onLoginCanceled()
{
    this->closeWindow();
}
